package main

import (
	"context"
	"log"
	"net"

	"google.golang.org/grpc"

	pb "gitee.com/mayxiangfan/go-study/rpc/gRPC/proto"
)

type UserInfoService struct {
}

// 实现user.pb.go 里的接口
func (this *UserInfoService) GetUserInfo(c context.Context, request *pb.UserRequest) (response *pb.UserResponse, err error) {
	users := []string{"1111", "222"}
	response = &pb.UserResponse{}
	if request.Id == 0 {
		response.Name = users[0:]
	} else {
		response.Name = users[0:1]
	}
	return response, nil
}

func main() {
	//定义监听端口
	listen, err := net.Listen("tcp", ":8081")
	if err != nil {
		log.Fatal(err.Error())
	}

	//启动grpc服务
	server := grpc.NewServer()
	//注册用户服务
	pb.RegisterUserServiceServer(server, &UserInfoService{})
	log.Fatal(server.Serve(listen))
}
